package com.cleaningcloud.erp.controller;

import com.cleaningcloud.common.core.utils.poi.ExcelUtil;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import com.cleaningcloud.common.log.annotation.Log;
import com.cleaningcloud.common.log.enums.BusinessType;
import com.cleaningcloud.common.security.annotation.RequiresPermissions;
import com.cleaningcloud.erp.domain.ErpSale;
import com.cleaningcloud.erp.domain.dto.ErpSaleDto;
import com.cleaningcloud.erp.domain.excelExport.SaleExcelExport;
import com.cleaningcloud.erp.domain.statistics.SummaryStock;
import com.cleaningcloud.erp.domain.vo.ErpSaleDetailsVo;
import com.cleaningcloud.erp.domain.vo.ErpSaleVo;
import com.cleaningcloud.erp.service.IErpSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 销售单Controller
 *
 * @author cleaningcloud
 * @date 2024-10-02
 */
@RestController
@RequestMapping("/ErpSale")
public class ErpSaleController extends BaseController {
    @Autowired
    private IErpSaleService erpSaleService;

    /**
     * 查询销售单列表
     */
    @RequiresPermissions("erp:ErpSale:list")
    @GetMapping("/list")
    public TableDataInfo list(ErpSaleDto erpSale) {
        startPage();
        List<ErpSaleVo> list = erpSaleService.selectErpSaleList(erpSale);
        return getDataTable(list);
    }

    /**
     * 导出销售单列表
     */
    @RequiresPermissions("erp:ErpSale:export")
    @Log(title = "销售单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ErpSaleDto erpSale) {
        List<ErpSaleVo> list = erpSaleService.selectErpSaleList(erpSale);
        ExcelUtil<ErpSaleVo> util = new ExcelUtil<ErpSaleVo>(ErpSaleVo.class);
        util.exportExcel(response, list, "销售单数据");
    }

    /**
     * 导出销售单明细列表
     */
    @RequiresPermissions("erp:ErpSale:export")
    @Log(title = "销售单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDetails/{saleId}")
    public void exportDetails(HttpServletResponse response, @PathVariable Long saleId) {
        ErpSaleVo erpSaleVo = erpSaleService.selectErpSaleBySaleId(saleId);
        List<SaleExcelExport> list = new ArrayList<>();
        for (ErpSaleDetailsVo erpSaleDetailsVo : erpSaleVo.getDetailsList()) {
            SaleExcelExport saleExcelExport = new SaleExcelExport();
            saleExcelExport.setSaleOdd(erpSaleVo.getSaleOdd());
            saleExcelExport.setCustomerName(erpSaleVo.getCustomerName());
            saleExcelExport.setSaleTime(erpSaleVo.getSaleTime());
            saleExcelExport.setLogisticsCost(erpSaleVo.getLogisticsCost());
            saleExcelExport.setOtherCost(erpSaleVo.getOtherCost());
            saleExcelExport.setLogisticsStauts(erpSaleVo.getLogisticsStauts());
            saleExcelExport.setNickName(erpSaleVo.getNickName());
            saleExcelExport.setPhonenumber(erpSaleVo.getPhonenumber());
            saleExcelExport.setPayAmount(erpSaleVo.getPayAmount());
            saleExcelExport.setRateMoney(erpSaleVo.getRateMoney());
            saleExcelExport.setApproveName(erpSaleVo.getApproveName());
            saleExcelExport.setApproveTime(erpSaleVo.getApproveTime());
            saleExcelExport.setCreateName(erpSaleVo.getCreateName());
            saleExcelExport.setCommodityNo(erpSaleDetailsVo.commodityNo);
            saleExcelExport.setCommodityName1(erpSaleDetailsVo.getCommodityName1());
            saleExcelExport.setCommodityType(erpSaleDetailsVo.getCommodityType());
            saleExcelExport.setCommoditySpec(erpSaleDetailsVo.getCommoditySpec());
            saleExcelExport.setUnitName1(erpSaleDetailsVo.getUnitName1());
            saleExcelExport.setUnitResult(erpSaleDetailsVo.getUnitResult());
            saleExcelExport.setCostPrice(erpSaleDetailsVo.getCostPrice());
            saleExcelExport.setSalePrice(erpSaleDetailsVo.getSalePrice());
            saleExcelExport.setSaleNumber(erpSaleDetailsVo.getSaleNumber());
            list.add(saleExcelExport);
        }
        ExcelUtil<SaleExcelExport> util = new ExcelUtil<SaleExcelExport>(SaleExcelExport.class);
        util.exportExcel(response, list, "销售单数据");
    }

    /**
     * 获取销售单详细信息
     */
    @RequiresPermissions("erp:ErpSale:query")
    @GetMapping(value = "/{saleId}")
    public AjaxResult getInfo(@PathVariable("saleId") Long saleId) {
        return success(erpSaleService.selectErpSaleBySaleId(saleId));
    }

    /**
     * 新增销售单
     */
    @RequiresPermissions("erp:ErpSale:add")
    @Log(title = "销售单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ErpSaleDto erpSale) {
        return toAjax(erpSaleService.insertErpSale(erpSale));
    }

    /**
     * 修改销售单
     */
    @RequiresPermissions("erp:ErpSale:edit")
    @Log(title = "销售单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ErpSaleDto erpSale) {
        return toAjax(erpSaleService.updateErpSale(erpSale));
    }

    /**
     * 修改销售单
     */
    @RequiresPermissions("erp:ErpSale:edit")
    @Log(title = "更改物流状态", businessType = BusinessType.UPDATE)
    @PutMapping("/logistics")
    public AjaxResult logistics(@RequestBody ErpSaleDto erpSale) {
        return toAjax(erpSaleService.logistics(erpSale));
    }

    /**
     * 删除销售单
     */
    @RequiresPermissions("erp:ErpSale:remove")
    @Log(title = "销售单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{saleIds}")
    public AjaxResult remove(@PathVariable Long[] saleIds) {
        return toAjax(erpSaleService.deleteErpSaleBySaleIds(saleIds));
    }


    /**
     * 销售单审核
     */
    @RequiresPermissions("erp:ErpProcure:audit")
    @Log(title = "销售单审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody ErpSale erpSale) {
        return toAjax(erpSaleService.auditSale(erpSale));
    }


    /**
     * 汇总备货查询
     */
    @RequiresPermissions("erp:ErpSale:query")
    @GetMapping(value = "/getSummaryStock")
    public AjaxResult getSummaryStock(SummaryStock summaryStock) {
        return success(erpSaleService.getSummaryStock(summaryStock));
    }


    /**
     * 导出汇总备货
     */
    @RequiresPermissions("erp:ErpSale:export")
    @PostMapping("/exportSummaryStock")
    public void exportSummaryStock(HttpServletResponse response) {
        List<SummaryStock> list = erpSaleService.getSummaryStock(new SummaryStock());
        ExcelUtil<SummaryStock> util = new ExcelUtil<SummaryStock>(SummaryStock.class);
        util.exportExcel(response, list, "汇总备货");
    }
}
